/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package components;

import java.net.*;
import java.io.*;
import SRDThread.*;
import SRDException.*;
import commands.drepComandos;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ylopez
 */
public class drep {
    private static final int PORT = 43718;
    private static final int PORTDMON = 43665;
    private static List threatIds;

    public static void main(String [] args) throws SRDException{
        int port;
        String hostDmon;
        int portDmon;
        String directory;
        ServerSocket serverSocket;
        boolean listening;

        port = PORT;
        hostDmon = null;
        portDmon = PORTDMON;
        directory = System.getProperty("user.dir");
        serverSocket = null;
        listening = true;
        InetAddress ownIP = null;

        try {
            if (args.length == 8 && args[0].equals("-c") && args[2].equals("-h") &&
                    args[4].equals("-p") && args[6].equals("-d")) {

                port = Integer.parseInt(args[1]);
                hostDmon = args[3];
                portDmon = Integer.parseInt(args[5]);
                directory = args[7];
                System.setProperty("user.dir", directory);
            } else {
                SRDException srde = new SRDException("Llamada a drep invalida. "
                        + "Todos los argumentos son obligatorios");
                srde.printException();
                System.exit(-1);
            }
        } catch (NumberFormatException nfe) {
            //TODO Manejar esta excepcion bien
            System.err.println("Alguno de los 2 puertos no es un numero entero.");
            nfe.printStackTrace();
            System.exit(-1);
        }

        try {
            serverSocket = new ServerSocket(port);
            System.out.println("Conectado al puerto: " + port);
        } catch (IOException e) {
            System.err.println("No se puede conectar al puerto: " + port);
            System.exit(-1);
        }

        InetAddress inetAddDrep = null;
        Socket socket = null;
        PrintWriter out = null;
        BufferedReader in = null;
        try {
            String userName = System.getProperty("user.name");
            inetAddDrep = InetAddress.getByName(hostDmon);
            socket = new Socket(inetAddDrep, portDmon);
            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("La direcci'on " + inetAddDrep.getHostAddress() +
                    ": " + inetAddDrep.getHostName() + " no se encuentra");
            System.exit(1);
        } catch (IOException e) {
            System.err.println(drep.class.getName() + ": Error de E/S esperando coneccion de clientes");
            System.exit(1);
        }

        try {
            ownIP = InetAddress.getLocalHost();
        } catch (UnknownHostException ex) {
            Logger.getLogger(drep.class.getName()).log(Level.SEVERE, null, ex);
        }

        String[] connectParameter = new String[]{ownIP.toString(),(new Date()).toString()};
        try {
            drepComandos.connect(connectParameter, out, in);
        } catch (SRDException ex) {
            ex.printException();
            ex.printStackTrace();
            System.exit(-1);
        }

        SRDThread principalThread = new SRDThread(null, hostDmon, portDmon, directory, true, ownIP, null, "drep", null, out, in, null);
        principalThread.start();
        try {
            while (listening){
                new SRDThread(serverSocket.accept(), hostDmon, portDmon, directory, false, ownIP, null, "drep", null, out, in, null).start();
            }

            serverSocket.close();
        } catch (IOException ex) {
            System.err.println(drep.class.getName() + ": Error de E/S esperando coneccion de clientes");
        }
    }
}
